<template>
	<view class="order-container">
	  <!--地址-->
	  <view class="order-address-info" @click="onAddress">
		<view v-if="addressInfo.name" class="contact-box">
	        <view>
				<view class="contact">
					<view>
						<image src="../../static/icon/user.png"></image>
						<text class="val">{{addressInfo.name}}</text>
					</view>
					<view class="mobile-box">
						<image src="../../static/icon/mobile.png"></image>
						<text  class="val">{{addressInfo.mobile}}</text>
					</view>
				</view>
				<view class="detail">{{addressInfo.totalDetail}}</view>
			</view>
			<view class="contact-icon" v-if="orderStatus == 0">
				<image src="../../static/icon/arrow@right.png"></image>
			</view>
		</view>
		<view v-else class="add-new-address">
			<text class="icon">+</text>
			<text>添加地址</text>
		</view>
	  </view>
	
	  <!--列表-->
	  <view class="order-main">
	      <view v-for="(item, index) in cartList" v-key="index" class="product-item">
	        <view class="item-left">
	          <image :src="item.image"></image>
	        </view>
	        <view class="item-middle">
	          <view>{{item.name}}</view>
	          <view class="sku-box">
	            <!-- <text v-for="(item, index) in item.props_text" v-key="index">{{item}}</text> -->
				<text>{{item.props_text}}</text>
	          </view>
	          <view>￥{{item.price}}</view>
	        </view>
	        <view class="item-right">
	          ×{{item.num}}
	        </view>
	      </view>
	  </view>
	
	  <!-- <view class="invoice-box" bindtap="enterInvoice">
	    <view class="top">
	      <view class="right">发票信息</view>
	      <view class="left">
	        <text>{{invoiceParams.title}}</text>
	        <image src="/static/right.png"></image>
	      </view>
	    </view>
	    <view v-if="invoiceParams.type != 0" class="content">
	        <view v-for="(item, index) in invoiceParams.detail" v-key='index'>
	          <text style="color: #666;">{{item.title}}：</text>
	          <text style="color: #333;">{{item.value}}</text>
	        </view>
	    </view>
	    <view class="line"></view>
	  </view> -->
	  <view class="note">
	    <view class="title">备注：<text class="basecolor">(选填)</text></view>
	    <view class="len">{{briefLength}}/100字</view>
	    <view class="content">
	      <textarea maxlength="100" @input="getBriefLen" placeholder-style="font-size:24rpx;" placeholder="有额外要求请说明,最多100字..."/>
	    </view>
	  </view>
	  <!--
	  <view class="note">
	    <view class="content">
	      <view>备注：</view>
	      <textarea maxlength="100" bindinput="getBriefLen" placeholder-style="font-size:24rpx;"
	       placeholder="有额外需求请备注通知商家,最多100字..."/>
	    </view>
	    <view class="len">{{briefLength}}/100字</view>
	  </view>
	  -->
	  <!-- <view class="total-box">
	    <view><text>商品总价</text><text>￥1188.00</text></view>
	    <view><text>运费</text><text>￥0.00</text></view>
	    <view><text>合计</text><text>￥1188.00</text></view>
	  </view> -->
	  <!--结算-->
	  <view class="footer-account-box">
	    <view class="total-account">
	      合计：￥{{ totalPrice }}
	    </view>
	    <view class="pay" :class="!addressInfo ? 'disabled': ''" @click="add()">提交订单</view>
	  </view>
	</view>
		
</template>

<script>
	import { Address } from '@/utils/model/address-model.js';
	import { Cart } from '@/utils/model/cart-model.js';
	import { Goods } from '@/utils/model/goods-model.js';
	import { Order } from '@/utils/model/order-model.js';
	var address = new Address(); 
	var cart = new Cart(); //实例化 购物车
	var goods = new Goods();
	var order = new Order(); 
	
	export default{
		data(){
			return{
				addressInfo:{},
				ids: '',
				is_new: 0,
				brief: '',
				briefLength: 0,
				//invoiceParams: null,
				cartList: [],
				totalPrice: 0
			}
		},
		onLoad(options) {
			let that = this;
			//let list = JSON.parse(options.list);
			//let account = options.account;
			//that.list = list
			//this.account = account
			//console.log(list)
			this.ids = options.ids
			this.is_new = options.is_new
			this.getCartList();
			//let userId = uni.getStorageSync("userId");
			//查询当前购物车商品中是否存在售罄商品，存在则只能提示预定
			/* let goods_ids = '';
			for(let i = 0; i < that.cartData.goodsList.length; i++){
				let id = that.cartData.goodsList[i].id;
				goods_ids += id + ',';
			}
			goods_ids = goods_ids.substring(0, goods_ids.lastIndexOf(',')); */
		},
		onShow: function () {
		    /* if(!this.invoiceParams){
		      this.setData({
		        invoiceParams: {
		          type: 0,
		          title:'不开具发票',
		          open: false,
		          detail:[],
		        }
		      })
		    }*/
			this.getAddress();
		},
		methods:{
			async getCartList() {
				let result = await cart.getList(this.ids,this.is_new);
				this.cartList = result.data.list;
				this._calcTotalPriceAndTotalCount()
			},
			async getAddress() {
				let result = await address.getDefault();
				if(result.code == 1){
					this._bindAddressInfo(result.data.detail);
				}else{
					this.addressInfo = {}
				}
			},
			onAddress() {
			    uni.navigateTo({
			      url: '/pages/my/address/index',
			    })
			    
			},
			
			bindtapAddress(){
				console.log('aaaaaaaaa')
				uni.navigateTo({
					url: '../address/index'
				})
			},
			
			getBriefLen(e){
			    let len = e.detail.cursor;
			    let brief = e.detail.value;
			    this.briefLength = len
			    this.brief = brief
			},
			//提交订单
			add(){
				//微信消息通知
				//商家接单通知
				//退款进度通知
				//let total_price = 0;//订单总价
				//total_price = this.account;//订单总价
				let user_note = this.brief;//用户备注
				if (!this.addressInfo || !this.addressInfo.name) {
					uni.showToast({
						title: '请填写您的收货地址'
					})
					return;
				}   
							  
				let orderInfo = {
					cart_ids: this.ids,
					//total_price: total_price,
					user_note: user_note,
					address_id: this.addressInfo.id,
					is_new: this.is_new
				}
				console.log(orderInfo);
				
				order.add(orderInfo).then((res) => {
					console.log(res)
					console.log('mmmmmmmmmmmmmmmmmmmmmmmmm')
					if(res.code == 1){
						let orderId = res.data.id;
						uni.redirectTo({
							url: '/pages/order/pay?id=' + orderId
						})
					}else{
						uni.showToast({
							title: res.msg
						})
					}
				}).catch((err) => {
					console.log(err)
					console.log('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')
				})
			},
			_bindAddressInfo(addressInfo) {
				let totalDetail = address.setAddressInfo(addressInfo)
				addressInfo.totalDetail = totalDetail
				this.addressInfo = addressInfo || {}
			},
			
			/*
			*	计算总金额和选择的商品总数
			* */
			_calcTotalPriceAndTotalCount(){
				let totalPrice = 0;
				let totalCount = 0;
				let multiple = 100;
				let list = this.cartList;
				let leg = list.length;
				
				console.log(list);
				console.log('aaaaaaaaaaaaaaaaaaaaa')
				for(let i = 0; i < leg; i++){
					//避免 0.05 + 0.01 = 0.060 000 000 000 000 005 的问题，乘以 100 *100
					totalPrice += Number(list[i].price) * multiple * list[i].num * multiple;
					console.log(totalPrice)
					totalCount += list[i].num;
				}
				this.totalCount = totalCount;
				this.totalPrice = totalPrice / (multiple*multiple);
			},	
		}
	}
</script>

<style>
	page{
		background-color: #F4F6FA;
		padding-bottom: 160rpx;
	}
</style>
 
<style scoped lang="scss">
	@import '~@/pages/order/add.scss';
</style>


